using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using MicroMsg.Storage;
using System;
using System.IO;
namespace MicroMsg.Scene
{
	public class NetSceneEmojiDownload : NetSceneBaseEx<DownloadEmojiRequest, DownloadEmojiResponse, DownloadEmojiRequest.Builder>
	{
		private const string TAG = "NetSceneEmojiDownload";
		public const int DOSCENE_SUCCESS = 1;
		public const int DOSCENE_SYS_ERR = 0;
		public const int DOSCENE_LOADING_ERR = -1;
		private string mUserName = "";
		private int ONE_PACK_DOWNLOADSIZE = 8192;
		private EmojiDownloadInfo mEmojiDownInfo;
		private MemoryStream mReceiveSream;
		private ChatMsg mMsg;
		private int doSceneCount;
		public bool doScene(EmojiDownloadInfo emojiDownInfo, ChatMsg chatMsg_0)
		{
			if (emojiDownInfo != null && !string.IsNullOrEmpty(emojiDownInfo.strMd5) && !string.IsNullOrEmpty(emojiDownInfo.strID))
			{
				this.mEmojiDownInfo = emojiDownInfo;
				this.mMsg = chatMsg_0;
				return this.doScene();
			}
			return false;
		}
		private bool doScene()
		{
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			EmojiDownloadInfoReq emojiDownloadInfoReq = new EmojiDownloadInfoReq.Builder
			{
				ID = this.mEmojiDownInfo.strID,
				MD5 = this.mEmojiDownInfo.strMd5,
				StartPos = this.mEmojiDownInfo.nStartPos,
				TotalLen = this.mEmojiDownInfo.nTotalLen
			}.Build();
			this.mBuilder.EmojiItemList.Add(emojiDownloadInfoReq);
			this.mBuilder.SetEmojiItemCount(this.mBuilder.EmojiItemList.get_Count());
			this.mSessionPack.mCmdUri = "/cgi-bin/micromsg-bin/receiveemoji";
			this.mSessionPack.mConnectMode = 2;
			base.endBuilder();
			this.doSceneCount++;
			return true;
		}
		protected override void onFailed(DownloadEmojiRequest request, DownloadEmojiResponse response)
		{
			Log.smethod_1("NetSceneEmojiDownload", "system error DownLoad emoji image failed !");
			EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOAD_EMJI_IMG_FAIL, null, null);
		}
		protected override void onSuccess(DownloadEmojiRequest request, DownloadEmojiResponse response)
		{
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			EmojiDownloadInfoResp emojiDownloadInfoResp = response.EmojiItemList.get_Item(0);
			if (ret != RetConst.MM_OK || emojiDownloadInfoResp.Ret != 0)
			{
				Log.smethod_1("NetSceneEmojiDownload", "Download emoji image failed because of net error");
				EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOAD_EMJI_IMG_FAIL, null, null);
				return;
			}
			if (emojiDownloadInfoResp.MD5 != this.mEmojiDownInfo.strMd5)
			{
				Log.smethod_1("NetSceneEmojiDownload", "Download emoji image failed because of resp.MD5 != mEmojiDownInfo.strMd5");
				EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOAD_EMJI_IMG_FAIL, null, null);
				return;
			}
			if (emojiDownloadInfoResp.StartPos < 0 || emojiDownloadInfoResp.StartPos > emojiDownloadInfoResp.TotalLen || emojiDownloadInfoResp.TotalLen <= 0)
			{
				Log.smethod_1("NetSceneEmojiDownload", "Download emoji image failed because of server image length error!");
				EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOAD_EMJI_IMG_FAIL, null, null);
				return;
			}
			if (emojiDownloadInfoResp.StartPos != this.mEmojiDownInfo.nTransLen)
			{
				Log.smethod_1("NetSceneEmojiDownload", "Download image failed because of position error!");
				EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOAD_EMJI_IMG_FAIL, null, null);
				return;
			}
			if (emojiDownloadInfoResp.TotalLen > 0)
			{
				this.mEmojiDownInfo.nTotalLen = emojiDownloadInfoResp.TotalLen;
			}
			if (!this.writeBufIntoStream(emojiDownloadInfoResp.EmojiBuffer.Buffer.ToByteArray(), emojiDownloadInfoResp.StartPos, emojiDownloadInfoResp.EmojiBuffer.Buffer.ToByteArray().Length))
			{
				Log.smethod_1("NetSceneEmojiDownload", "Download emoji image failed because writeBufIntoStream failed!");
				EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOAD_EMJI_IMG_FAIL, null, null);
				return;
			}
			this.mEmojiDownInfo.nTransLen += emojiDownloadInfoResp.EmojiBuffer.Buffer.ToByteArray().Length;
			this.mEmojiDownInfo.nStartPos = this.mEmojiDownInfo.nTransLen;
			if (!this.isDownloadCompleted())
			{
				if (this.doSceneCount < 10)
				{
					this.doScene();
				}
				return;
			}
			Log.smethod_4("NetSceneEmojiDownload", "Download image Complete!");
			string text = StorageIO.getImagePath() + "/" + this.mEmojiDownInfo.strMd5;
			if (StorageIO.writeToFile(text, this.mReceiveSream.GetBuffer(), false))
			{
				if (this.mMsg != null)
				{
					this.mMsg.strThumbnail = text;
					this.mMsg.strPath = text;
					StorageMgr.chatMsg.updateByMsgSvrID(this.mMsg);
				}
				EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOADIMGD_EMJI_COMPLETE, this.mMsg, null);
				return;
			}
			EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOAD_EMJI_IMG_FAIL, null, null);
		}
		private bool isDownloadCompleted()
		{
			return this.mEmojiDownInfo.nTransLen >= this.mEmojiDownInfo.nTotalLen;
		}
		private bool writeBufIntoStream(byte[] byte_0, int startPos, int ndataLen)
		{
			if (byte_0 == null)
			{
				return false;
			}
			if (this.mReceiveSream == null)
			{
				this.mReceiveSream = new MemoryStream();
			}
			bool result;
			try
			{
				this.mReceiveSream.Seek((long)startPos, 0);
				this.mReceiveSream.Write(byte_0, 0, ndataLen);
				result = true;
			}
			catch (Exception ex)
			{
				Log.smethod_1("NetSceneEmojiDownload", ex.get_Message());
				result = false;
			}
			return result;
		}
	}
}
